//! # Color
//!
//! 卡牌花色定义
//!

/// 定义花色类型
#[derive(Debug, Clone, PartialEq)]
pub enum Color {
    /// 未知类型，一般表示出错
    Unknown,

    /// Heart
    ///
    /// 红桃 ♥
    Heart,

    /// Diamond
    ///
    /// 方块 ♦
    Diamond,

    /// Spade
    ///
    /// 黑桃 ♠
    Spade,

    /// Club
    ///
    /// 梅花 ♣
    Club,
}

impl Default for Color {
    fn default() -> Self {
        Self::Unknown
    }
}

impl std::fmt::Display for Color {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::Club => "♣",
                Self::Heart => "♥",
                Self::Spade => "♠",
                Self::Diamond => "♦",
                Self::Unknown => "Unknown",
            }
        )
    }
}

impl TryFrom<&str> for Color {
    type Error = crate::ParseStrError;

    fn try_from(text: &str) -> Result<Color, Self::Error> {
        match text {
            "红桃" | "HEART" | "Heart" | "heart" | "♥" => Ok(Self::Heart),

            "方块" | "方片" | "DIAMOND" | "Diamond" | "diamond" | "♦" => Ok(Self::Diamond),

            "黑桃" | "SPADE" | "Spade" | "spade" | "♠" => Ok(Self::Spade),

            "梅花" | "CLUB" | "Club" | "club" | "♣" => Ok(Self::Club),

            _ => Err(crate::ParseStrError::new(crate::ParseType::Color, text)),
        }
    }
}

impl Color {
    /// 花色是否是红色（红桃、方块）
    pub fn is_red(&self) -> bool {
        match self {
            Color::Heart | Color::Diamond => true,
            _ => false,
        }
    }

    /// 花色是否是黑色（黑桃、梅花）
    pub fn is_black(&self) -> bool {
        match self {
            Color::Spade | Color::Club => true,
            _ => false,
        }
    }
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn test_color_is_red() {
        assert_eq!(Color::Heart.is_red(), true);
        assert_eq!(Color::Spade.is_red(), false);
    }
}
